﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using VO.Business.Controllers;
using VO.Entities;

namespace VO.Web.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            RegisterHyperLink.NavigateUrl = "/signup";
            LoginUser.Focus();
        }

        protected void LoginUser_LoggedIn(object sender, EventArgs e)
        {
            //check VO user existance
            MembershipUser mUser = Membership.GetUser(LoginUser.UserName);
            Guid idMembership = new Guid(mUser.ProviderUserKey.ToString());
            User _user = UsersController.GetUserByIdMembership(idMembership);
            if (_user == null)
            {
                //create user in VO
                User newUser = new User();
                newUser.IdMembership = idMembership;
                newUser.UserDescription = string.Empty;
                newUser.PublicName = mUser.UserName;
                UsersController.SaveUser(newUser, true);
            }
        }
    }
}
